package com.control.qct.businesssteward.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.activity.AccountDetailActivity;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.utils.DateUtil;
import com.control.qct.businesssteward.utils.PopShopSelectUtils;
import com.control.qct.businesssteward.utils.PopUtils;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 侧滑菜单--门店及日结/月结日期选择
 * Created by Administrator on 2017/12/14 0014.
 */

@SuppressLint("ValidFragment")
public class AccountDateSelectFragment extends BaseFragment {
    //    @BindView(R.id.tv_shop_select)
//    TextView mTvShopSelect;
    @BindView(R.id.tv_account_date_title)
    TextView mTvAccountDateTitle;
    @BindView(R.id.tv_day_account_date)
    TextView mTvDayAccountDate;
    @BindView(R.id.tv_month_account_date1)
    TextView mTvMonthAccountDate1;
    @BindView(R.id.tv_month_account_date2)
    TextView mTvMonthAccountDate2;
    @BindView(R.id.ll_month_account_date)
    LinearLayout mLlMonthAccountDate;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_sure)
    TextView mTvSure;
    private AccountDetailActivity mAccountDetailActivity;
    private String mDate;//选择的日期
    private int mId;
    private String mStoreId;
    private String mStoreName;
    private ICallbackDateListener mICallbackDateListener;
//    private PopShopSelectUtils mPopShopSelectUtils;

    public AccountDateSelectFragment(int id, ICallbackDateListener listener) {
        mId = id;
        mICallbackDateListener = listener;
    }

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_account_date_select, container, false);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mAccountDetailActivity = (AccountDetailActivity) context;
    }

    @Override
    protected void initView(View view) {
        mTvAccountDateTitle.setText(mId == 1 ? "日结时间" : "月结时间");
        mTvDayAccountDate.setVisibility(mId == 1 ? View.VISIBLE : View.GONE);
        mLlMonthAccountDate.setVisibility(mId == 2 ? View.VISIBLE : View.GONE);
        //设置门店数据
//        mPopShopSelectUtils = new PopShopSelectUtils();
//        mPopShopSelectUtils.registerListener(this, mNetProtocol, activity);
    }

    @OnClick({R.id.tv_day_account_date, R.id.tv_month_account_date1, R.id
            .tv_month_account_date2, R.id.tv_cancel, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.tv_shop_select://选择门店
//                mPopShopSelectUtils.setBottomPop(mAccountDetailActivity, R.id.fl_content);
//                break;
            case R.id.tv_day_account_date://日结日期选择
                setAccountDateSelectPop(mTvDayAccountDate);
                break;
            case R.id.tv_month_account_date1://月结日期一选择
                setAccountDateSelectPop(mTvMonthAccountDate1);
                break;
            case R.id.tv_month_account_date2://月结日期二选择
                setAccountDateSelectPop(mTvMonthAccountDate2);
                break;
            case R.id.tv_cancel://取消
                mAccountDetailActivity.mDlMenu.closeDrawers();
                break;
            case R.id.tv_sure://确定
                if (mId == 1) {
                    if (TextUtils.isEmpty(mTvDayAccountDate.getText().toString())) {
                        showShortToast("请选择日期！");
                        return;
                    }
                    mICallbackDateListener.onCallbackDate(mTvDayAccountDate.getText().toString(), "", mStoreId,
                            mStoreName);
                } else {
                    if (TextUtils.isEmpty(mTvMonthAccountDate1.getText().toString())) {
                        showShortToast("请选择起始日期！");
                        return;
                    }
                    if (TextUtils.isEmpty(mTvMonthAccountDate2.getText().toString())) {
                        showShortToast("请选择结束日期！");
                        return;
                    }
                    mICallbackDateListener.onCallbackDate(mTvMonthAccountDate1.getText().toString(),
                            mTvMonthAccountDate2.getText().toString(), mStoreId, mStoreName);
                }
                mAccountDetailActivity.mDlMenu.closeDrawers();
                break;
        }
    }

    /**
     * 设置日期选择器
     *
     * @param tv
     */
    private void setAccountDateSelectPop(final TextView tv) {
        View view = View.inflate(mAccountDetailActivity, R.layout.pop_account_date_select, null);
        final PopupWindow pop = PopUtils.getInstance().getBottomPop(mAccountDetailActivity, view);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        TextView tvSure = view.findViewById(R.id.tv_sure);
        DatePicker dpDateSelect = view.findViewById(R.id.dp_date_select);
        //设置中文日历
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (!TextUtils.isEmpty(tv.getText().toString())) {
            String[] time = tv.getText().toString().split("-");
            year = Integer.parseInt(time[0]);
            month = Integer.parseInt(time[1]) - 1;
            day = Integer.parseInt(time[2]);
        }
        mDate = year + "-" + (month + 1) + "-" + day;
        dpDateSelect.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
                mDate = year + "-" + (month + 1) + "-" + day;
            }
        });
        //设置最大日期--当前时间
        GregorianCalendar g = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dpDateSelect.setMaxDate(g.getTimeInMillis());
        dpDateSelect.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop.dismiss();
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(mTvMonthAccountDate1.getText().toString()) && tv.getId() !=
                        mTvMonthAccountDate1.getId()) {
                    if (DateUtil.stringToMillis(mDate) < DateUtil.stringToMillis(mTvMonthAccountDate1.getText()
                            .toString())) {
                        showShortToast("不能小于起始时间!");
                        return;
                    }
                }
                tv.setText(mDate);
                pop.dismiss();
            }
        });
        //设置PopupWindow显示位置
        pop.showAtLocation(mAccountDetailActivity.findViewById(R.id.fl_content), Gravity.BOTTOM | Gravity
                .CENTER_HORIZONTAL, 0, 0);
    }

//    @Override
//    public void onSure(String storeId, String storeName) {
//        mStoreId = TextUtils.isEmpty(storeId) ? "" : storeId;
//        mStoreName = TextUtils.isEmpty(storeName) ? "" : storeName;
//        mTvShopSelect.setText(TextUtils.isEmpty(storeName) ? "门店" : storeName);
//    }

    /**
     * 返回
     */
    public interface ICallbackDateListener {
        void onCallbackDate(String firstDate, String endDate, String storeId, String storeName);
    }
}
